1 module hip.api.data.textureatlas;
2 public import hip.api.renderer.texture;
3 
4 
5 struct AtlasSize
6 {
7     uint width, height;
8     alias w = width, h = height;
9 }
10 struct AtlasRect
11 {
12     float x, y, width, height;
13     alias w = width, h = height;
14 
15     /**
16      *
17      * Params:
18      *   size = The entire atlas size.
19      * Returns: A TextureQuad compatible format from this atlas rect. Used for getting its vertices
20      */
21     TextureCoordinatesQuad toQuad(AtlasSize size)
22     {
23         return TextureCoordinatesQuad(
24             x / size.width,
25             y / size.height,
26             (x+width) / size.width,
27             (y+height) / size.height
28         );
29     }
30 
31     version(Have_data)
32     {
33         import hip.data.jsonc;
34         static AtlasRect fromJSON()(JSONValue v, bool useShortenedDimensionNames = true)
35         {
36             return AtlasRect(
37                 v["x"].get!float,
38                 v["y"].get!float,
39                 v[useShortenedDimensionNames ? "w" : "width"].get!float,
40                 v[useShortenedDimensionNames ? "h" : "height"].get!float
41             );
42         }
43     }
44 }
45 
46 struct AtlasFrame
47 {
48     string filename;
49     bool rotated;
50     bool trimmed;
51 
52     AtlasRect frame;
53     AtlasRect spriteSourceSize;
54     AtlasSize sourceSize;
55     IHipTextureRegion region;
56 
57     alias region this;
58 }
59 
60 interface IHipTextureAtlas
61 {
62     ref inout(AtlasFrame[string]) frames() inout ;
63 
64     alias frames this;
65 }